﻿using System.Management;

namespace TKH.BizTalk
{
    public class GroupSettings
    {
        #region Cache
        private static GroupSettings cache = null;
        private static object cacheLock = new object();

        private static GroupSettings Cache
        {
            get
            {
                if (cache == null)
                {
                    lock (cacheLock)
                    {
                        if (cache == null)
                        {
                            GroupSettings groupSettings = new GroupSettings();
                            groupSettings.Load();
                            cache = groupSettings;
                        }
                    }
                }
                return cache;
            }
        }

        public static string MgmtDbName
        {
            get { return Cache.mgmtDbName; }
        }
        public static string MgmtDbServer
        {
            get { return Cache.mgmtDbServer; }
        }
        public static string MsgBoxDbName
        {
            get { return Cache.msgboxDbName; }
        }
        public static string MsgBoxDbServer
        {
            get { return Cache.msgboxDbServer; }
        }
        public static string BamDbName
        {
            get { return Cache.bamDbName; }
        }
        public static string BamDbServer
        {
            get { return Cache.bamDbServer; }
        }
        public static string MgmtDbConnectionString
        {
            get { return CreateConnectionString(Cache.mgmtDbServer, Cache.mgmtDbName); }
        }
        public static string MsgBoxDbConnectionString
        {
            get { return CreateConnectionString(Cache.msgboxDbServer, Cache.msgboxDbName); }
        }
        public static string BamDbConnectionString
        {
            get { return CreateConnectionString(Cache.bamDbServer, Cache.bamDbName); }
        }
        private static string CreateConnectionString(string server, string dbname)
        {
            return string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", server, dbname);
        }
        #endregion

        #region Instance
        private string mgmtDbName;
        private string mgmtDbServer;
        private string msgboxDbName;
        private string msgboxDbServer;
        private string bamDbName;
        private string bamDbServer;

        private GroupSettings() { }

        private void Load()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\MicrosoftBizTalkServer", "SELECT * FROM MSBTS_GroupSetting");
            {
                foreach (ManagementObject Group in searcher.Get())
                {
                    if (Group != null)
                    {
                        Group.Get();
                        this.mgmtDbName = Group["MgmtDbName"].ToString();
                        this.mgmtDbServer = Group["MgmtDbServerName"].ToString();
                        this.bamDbName = Group["BamDbName"].ToString();
                        this.bamDbServer = Group["BamDbServerName"].ToString();
                    }
                }
            }
            searcher = new ManagementObjectSearcher(@"root\MicrosoftBizTalkServer", "SELECT * FROM MSBTS_MsgBoxSetting");
            {
                foreach (ManagementObject Group in searcher.Get())
                {
                    if (Group != null && (bool)Group["IsMasterMsgBox"])
                    {
                        Group.Get();
                        this.msgboxDbName = Group["MsgBoxDBName"].ToString();
                        this.msgboxDbServer = Group["MsgBoxDBServerName"].ToString();

                    }
                }
            }
        }
        #endregion
    }
}

